home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / 3D_GRAPH / G3D_DATA.C < prev    next >
Text File  |  1989-08-28  |  3KB  |  211 lines

  1. /*
  2.     Copyright '89    Christopher Moll
  3.     all rights reserved
  4. */
  5.  
  6.  
  7. #include    <ctype.h>
  8. #include    "graph3D.h"
  9. #ifndef    _LSC3_
  10. #    include    <FileMgr.h>
  11. #    include    <EventMgr.h>
  12. #    include    <StdFilePkg.h>
  13. #endif
  14.  
  15.  
  16. extern    int        numX, numY;
  17.  
  18. extern    Real    startX, startY;
  19. extern    Real    endX, endY;
  20. extern    Real    deltaX, deltaY;
  21. extern    int        numX, numY;
  22.  
  23. extern    Vector        maxVect, minVect;
  24.  
  25. extern    Real        *funcResults;
  26. extern    Point        *graphPoints;    /* same elements as funcResults */
  27.  
  28. extern    Boolean        vectrsCurrent;
  29.  
  30.  
  31. Boolean
  32. ReadData()
  33. {
  34. unsigned    char    rdBuffer[256], *inLine;
  35.     int        theErr;
  36.     int        ReadLine();
  37.     Real    atof();
  38.     Boolean        DoGetFile(), AllocatePts();
  39.     
  40.     if (NOT(DoGetFile()))
  41.         return(FALSE);
  42.  
  43.     theErr = ReadLine(256, rdBuffer);
  44.     if (theErr) {
  45.         NumToString((long)theErr, rdBuffer);
  46.         ParamText(rdBuffer, "\0", "\0", "\0");
  47.         Alert(ALRT_FILE, NIL);
  48.         return;
  49.     }
  50.  
  51.     inLine = rdBuffer;
  52.     
  53.     numX = atof(inLine);
  54.     while(NOT(isspace(*inLine)))
  55.         ++inLine;
  56.     while(isspace(*inLine))
  57.         ++inLine;
  58.     numY = atof(inLine);
  59.     startX = 0.0;
  60.     startY = 0.0;
  61.     endX = numX;
  62.     endY = numY;
  63.     deltaX = 2000.0;
  64.     deltaY = 2000.0;
  65.  
  66.     if (NOT(AllocatePts()))
  67.     {
  68.         MemAlert();
  69.         return;
  70.     }
  71.  
  72.     StoreDataPts();
  73.     CloseFile();
  74.  
  75.     vectrsCurrent = FALSE;
  76.     return(TRUE);
  77. }
  78.  
  79. static
  80. Boolean
  81. DoGetFile()
  82. {
  83. static    Point    where = {100, 100};
  84.     long        theList[4];
  85.     SFReply        theReply;
  86.     
  87.     theList[0] = 'TEXT';
  88.     
  89.     SFGetFile(where, "", NIL, 1, theList, NIL, &theReply);
  90.     
  91.     SetVol(NIL, theReply.vRefNum);
  92.     OpenFile(theReply.fName);
  93.     return(theReply.good);
  94. }
  95.  
  96. static    IOParam        dataFile;
  97.  
  98. static
  99. OpenFile(fileName)
  100. unsigned    char    *fileName;
  101. {
  102.     dataFile.ioCompletion = NIL;
  103.     dataFile.ioNamePtr = fileName;
  104.     dataFile.ioVRefNum = 0;
  105.     dataFile.ioPermssn = fsRdPerm;
  106.  
  107.     PBOpen(&dataFile, FALSE);
  108. }
  109.  
  110. static
  111. CloseFile()
  112. {
  113.     PBClose(&dataFile, FALSE);
  114. }
  115.  
  116. #define        newLnStop    0x80
  117.  
  118.  
  119. static
  120. int
  121. ReadLine(maxRead, rdBuffer)
  122. int    maxRead;
  123. char    *rdBuffer;
  124. {
  125.     dataFile.ioBuffer = rdBuffer;
  126.     dataFile.ioReqCount = maxRead;
  127.     dataFile.ioPosMode = fsAtMark + newLnStop + ('\r' << 8);
  128.  
  129.     PBRead(&dataFile, FALSE);
  130.     rdBuffer[dataFile.ioActCount - 1] = '\0';
  131.     return(dataFile.ioResult);
  132. }
  133.  
  134. #define    LN_LEN    1000
  135.  
  136. static
  137. StoreDataPts()
  138. {
  139.     register    int    xCnt, yCnt;
  140.     Real        currX, currY;
  141.     Real        maxFunc, minFunc, funcVal;
  142.     Real        xAtMax, yAtMax;
  143.     Real        xAtMin, yAtMin;
  144.     unsigned    char    rdBuffer[LN_LEN], *inLine;
  145.     int            theErr;
  146.     Real        atof(), NextVal();
  147.     Boolean        CmndPeriod();
  148.  
  149.     theErr = ReadLine(LN_LEN, rdBuffer);
  150.     inLine = rdBuffer;
  151.     funcVal = atof(inLine);
  152.     maxFunc = funcVal;
  153.     minFunc = funcVal;
  154.     xAtMax = startX;
  155.     yAtMax = startY;
  156.     xAtMin = startX;
  157.     yAtMin = startY;
  158.  
  159.     currX = startX;
  160.  
  161.     for (xCnt = 0; xCnt < numX; xCnt++)
  162.     {
  163.         currY = startY;
  164.         for (yCnt = 0; yCnt < numY; yCnt++)
  165.         {
  166.             funcVal = NextVal(&inLine);
  167.             funcResults[(long)xCnt * numY + (long)yCnt] = funcVal;
  168.             if (funcVal > maxFunc) {
  169.                 maxFunc = funcVal;
  170.                 xAtMax = currX;
  171.                 yAtMax = currY;
  172.             }
  173.             else if (funcVal < minFunc) {
  174.                 minFunc = funcVal;
  175.                 xAtMin = currX;
  176.                 yAtMin = currY;
  177.             }
  178.             currY += deltaY;
  179.         }
  180.         if (CmndPeriod())
  181.             break;  /* for */
  182.         currX += deltaX;
  183.         theErr = ReadLine(LN_LEN, rdBuffer);
  184.         inLine = rdBuffer;
  185.     }
  186.  
  187.     maxVect.x = xAtMax;
  188.     maxVect.y = yAtMax;
  189.     maxVect.z = maxFunc;
  190.     minVect.x = xAtMin;
  191.     minVect.y = yAtMin;
  192.     minVect.z = minFunc;
  193. }
  194.  
  195. static
  196. Real
  197. NextVal(str)
  198. unsigned    char    **str;
  199. {
  200.     Real    result, atof();
  201.  
  202.     result = atof(*str);
  203.  
  204.     while(NOT(isspace(**str)))
  205.         (*str)++;
  206.     while(isspace(**str))
  207.         (*str)++;
  208.  
  209.     return(result);
  210. }
  211.